<?php 
/**
 * 内容页控制器
 * @author 妮妮 <962611024@qq.com>
 */
class ContentController extends Controller{
    /**
     * 默认访问内容页方法
     */
    public function index(){
    	//p($_SESSION);
    	//p(Q('get.gid'));
    	$gid=Q('get.gid',0,'intval');
		//查询商品表和商品详细表 查到gid一条数据就ok
    	$goodsData=M()->join('__goods__ g JOIN __goods_detail__ gd ON g.gid=gd.gid')->where("g.gid={$gid}")->find();
		//库存
		if(!$goodsData)$this->error("不存在的页面",U('Index/Index/index'));
		foreach ($goodsData as $v) {
			if(K('GoodsList')->where("gid={$gid}")->getField('inventory',true)){
				//通过框架sum求和 压入一个下标
				$goodsData['inventory']=K('GoodsList')->where("gid={$gid}")->sum('inventory');
			}else{
				//如果没有库存默认为0
				$goodsData['inventory']=0;
			}
		}
		//大图裁分为数组 因为是一维数组，直接赋值就可以，不需要循环
		$goodsData['thumb']=explode(',', $goodsData['thumb']);
		//分配出去
		$this->assign('goodsData',$goodsData);
		//p($goodsData);
		//查找商品属性表和类型属性两张表 查到商品所对应的所有属性
		$listData=M()->join('__goods_attr__ ga JOIN __type_attr__ ta ON ga.taid=ta.taid')->where("gid={$gid}")->order('gaid')->all();
		
		//因为有共同的taid 这句是查询规格不被写死，分配出去，重新分组
		$attr= M()->join('__goods_attr__ ga JOIN __type_attr__ ta ON ga.taid=ta.taid')->where("gid={$gid} and taclass=1 ")->group("taname")->all();
		
		$this->assign('listData',$listData);
		$this->assign("attr",$attr);
		//p($attr);
		//p($listData);
		
        $this->display(); 
    }
	
	public function ajaxGoodsList(){
		
		
		$gid = Q("get.gid",0,"intval");
		//把右面逗号去除
		$str = rtrim($_POST['attr'],",");
		//组合成字符串
		$str = explode(",", $str);
		$combine = M("goods_list")->where("gid={$gid}")->all();
		
		foreach ($combine as $k => $v) {
			$tmp[$v['glid']] = explode(",",$v["combine"]);
			
		}
		if(!isset($str[0])||!isset($str[1])){
			$this->ajax(0);
		}
		foreach($tmp as $k=>$v){
				if(in_array($str[0],$v)&&in_array($str[1],$v)){
					$result = $k;
				}
			
		}
		
		$price = M("goods_attr")->where("gaid={$str[0]}")->getField("added")+M("goods_attr")->where("gaid={$str[1]}")->getField("added");
		
		
		$info = M("goods_list")->where("glid={$result}")->find();
//		p($info);
		$info["price"] = $price;
		$this->ajax($info);
		
	}
	
	/*
	 * 添加到购物车
	 * */
	public function addCart(){
		//把需要的值付给变量
		$glid = $_POST['glid'];  //1货品列表的glid
		$name = $_POST['name'];   //2 商品名字
		$price = $_POST['price'];  //3商品的价钱
		$num = $_POST['num'];  		//4商品的库存
		$combine = M("goods_list")->where("glid={$glid}")->getField("combine");
		$store = M("goods_list")->where("glid={$glid}")->getField("inventory");
		$temp  = M()->join("__goods_attr__ as ga inner join __type_attr__ as a on a.taid = ga.taid")->where ("gaid in($combine)")->all();
		
		foreach($temp as $k=>$v){
			
			$spec[] = array(
				"taname"=>$v['taname'],
				"gtvalue"=>$v['gtvalue']
			);
			
		}
		
		
		
		$thumb= $_POST['thumb'];
		//$size=M("goods_list")->where()->find();
		//p($size);
		//存在数组中
		$data = array(
			"id"=>$glid,
			"price"=>$price,
			"num"=>$num,
			"name"=>$name,
			'options'=>array(
				//这个是在货品列表里需要的字段，为了方便找到商品列表的glid
				"combine"=>$combine,
				//购物车需要的图片
				"thumb"=>$thumb,
				//尺寸和颜色
				"spec"=>$spec,
				"store"=>$store
			)
		);
		//用框架的方法直接添加到购物车
		Cart::add($data);
		//统计购物车中的商品数量 Cart::getTotalNums()
		$this->ajax( Cart::getTotalNums());
			
	}
	
	public function del(){
		$id =Q("get.id");
		Cart::del($id);
		$this->success("删除成功！",U("Cart/index"));
	}

	
	
 }
 
 
 
 
 
 
 
 
 
 
 




















 ?>